import http from "@/api/http";

export async function searchPosts(keywords) {
  try {
    const result = await http
      .post("/posts/search", { keyword: keywords })
      .then(function (response) {
        console.log(response);
        return response.data.posts;
      });
    return result;
  } catch (error) {}
}

export async function createPost(item) {
  try {
    const result = await http
      .post("/posts/create", item)
      .then(function (response) {
        return response.data.post;
      });
    return result;
  } catch (error) {}
}

export async function copyPost(item) {
  try {
    const result = await http
      .post("/posts/copy", item)
      .then(function (response) {
        return response.data;
      });
    return result;
  } catch (error) {}
}

export async function savePost(item) {
  try {
    const result = await http
      .post("/posts/save", item)
      .then(function (response) {
        return response.data.post;
      });
    return result;
  } catch (error) {}
}

export async function deletePost(item) {
  try {
    const result = await http
      .post("/posts/delete", item)
      .then(function (response) {
        return response.data.success;
      });
    return result;
  } catch (error) {}
}

export async function getPosts(page) {
  try {
    const result = await http
      .get(`/posts/get/${page}`)
      .then(function (response) {
        return response.data.posts;
      });
    return result;
  } catch (error) {}
}

export async function publishPost(plugin,post,token) {
  try {
    console.log(token);
    const result = await axios
      .post("/editor/publish", {
        plugin: plugin,
        post: post,
      },{'headers':{'Authorization':'Bearer '+token}})
      .then(function (response) {
        // console.log(response.data.post);
        return response.data.post;
      });
    // console.log(result)
    return result;
  } catch (error) {}
}


export async function uploadImg(imgForm) {
  try {
    
    const result = await http.post("/ai/upload", imgForm)
      .then(function(response){
        console.log('upload------------');
        return  response.data.img
      });
    return result
  } 
  catch (error) {
    console.log(error);
  }
}